package com.ly.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.vo.SysMessageTypeVO;
import com.ly.common.core.constant.BaseConstant;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.excel.ExcelUtil;
import com.ly.system.system.bean.dto.SysMessageDTO;
import com.ly.system.system.bean.entity.SysMessage;
import com.ly.system.system.bean.entity.SysMessageSend;
import com.ly.system.system.bean.vo.SysMessageVO;
import com.ly.system.service.ISysMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统消息
 *
 * @author jeecg
 * @Date: 2019-01-02
 */
@Tag(name = "系统通告维护")
@Slf4j
@RestController
@RequestMapping("/sys/msg")
@RequiredArgsConstructor
public class SysMessageController {

    /**
     * QQYUN-5072【性能优化】线上通知消息打开有点慢
     */
    private final ISysMessageService sysMessageService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<SysMessage> getId(@RequestParam(name = "id") String id) {
        return sysMessageService.getId(id);
    }

    /**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getPage")
    public R<IPage<SysMessage>> getPage(
            SysMessage entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return sysMessageService.getPage(entity, new Page<SysMessage>(pageNo, pageSize));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody SysMessage entity) {
        return sysMessageService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody SysMessage entity) {
        return sysMessageService.edit(entity);
    }

    /**
     * 修改-send
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改-send")
    @PostMapping("/editMsgSend")
    public R<String> editMsgSend(@RequestBody SysMessageSend entity) {
        return sysMessageService.editMsgSend(entity);
    }


    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysMessageService.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return sysMessageService.deleteBatch(ids);
    }

    /**
     * 发布
     *
     * @param id
     * @return
     */
    @GetMapping("/releaseMsg")
    public R<String> releaseMsg(@RequestParam(name = "id") String id) {
        return sysMessageService.releaseMsg(id);
    }

    /**
     * 撤销
     *
     * @param id
     * @return
     */
    @GetMapping("/revokeMsg")
    public R<String> revokeMsg(@RequestParam(name = "id") String id) {
        return sysMessageService.revokeMsg(id);
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @Operation(summary = "导出excel")
    @GetMapping("/exportXls")
    public void exportXls(HttpServletResponse response) {
        List<SysMessage> list = sysMessageService.list();
        ExcelUtil.exportExcel(list, "工作系数表", SysMessage.class, response);
    }

    /**
     * 导入excel
     *
     * @param file
     * @param updateSupport：是否删除标识
     * @return
     */
    @Operation(summary = "导入excel")
    @PostMapping("/importExcel")
    public R<?> importWork(MultipartFile file, boolean updateSupport) throws Exception {
        // 监听： new ImportListener(updateSupport);
        ExcelResult<SysMessage> result = ExcelUtil.importExcel(file.getInputStream(), SysMessage.class, null);
        return R.ok(true, result.getAnalysis());
    }

    /**
     * 根据消息发送记录ID获取消息内容
     *
     * @param id
     * @return
     */
    @GetMapping("/getMsgSend")
    public R<SysMessageVO> getMsgSend(@RequestParam(name = "id") String id) {
        return sysMessageService.getMsgSend(id);
    }

    /**
     * 获取未读消息通知数量
     *
     * @return
     */
    @GetMapping("/getMessageGroupCount")
    public R<List<SysMessageTypeVO>> getMessageGroupCount() {
        return sysMessageService.getMessageGroupCount();
    }

    /**
     * 【vue3用】 消息列表查询
     *
     * @param dto
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getMessagePage")
    public R<IPage<SysMessage>> getMessagePage(SysMessageDTO dto,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysMessageVO> page = new Page<SysMessageVO>(pageNo, pageSize);
        return sysMessageService.getMessagePage(page, dto);
    }


    /**
     * 更新用户系统消息阅读状态
     */
    @PostMapping("/updateReadStatusMessage")
    public R<SysMessageSend> updateReadStatusMessage(@RequestBody SysMessageDTO dto) {
        return sysMessageService.updateReadStatusMessage(dto);
    }

    /**
     * 清除当前用户所有未读消息
     *
     * @return
     */
    @PostMapping("/updateAllUnreadMessage")
    public R<String> updateAllUnreadMessage() {
        return sysMessageService.updateAllUnreadMessage();
    }

    /**
     * 通告查看详情页面（用于第三方APP）
     *
     * @param modelAndView
     * @param id
     * @return
     */
    @GetMapping("/show/{id}")
    public ModelAndView showContent(ModelAndView modelAndView, @PathVariable("id") String id) {
        SysMessage sysMessage = sysMessageService.getById(id);
        if (sysMessage != null && BaseConstant.MSG_SEND_STATUS_1.equals(sysMessage.getSendStatus())) {
            modelAndView.addObject("data", sysMessage);
            modelAndView.setViewName("sysMessage/showContent");
            return modelAndView;
        }
        modelAndView.setStatus(HttpStatus.NOT_FOUND);
        return modelAndView;
    }
}
